#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System;
using System.Security.Cryptography;
using System.Text;

namespace raya.Library
{
    public static class Utility
    {
        // constants
        public const string ADMINID = "1";
        public const string noPhotoThumbnailURL = "/images/people/nophoto.gif";
        public const int GADGET_WIDTH_CANVAS = 960;
        public const int GADGET_WIDTH_PROFILE = 656;

        static public string ConvertToASCII(string unicodeString)
        {
            byte[] unicodeBytes = Encoding.Unicode.GetBytes(unicodeString);
            byte[] asciiBytes = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, unicodeBytes);
            char[] asciiChars = new char[Encoding.ASCII.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
            Encoding.ASCII.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
            string asciiString = new string(asciiChars);

            return asciiString.Replace("?", "");
        }

        /**
        * Returns the "public" hash of the email address, i.e., the one we give out
        * to select partners via our API.
        *
        * @param  string _email An email address to hash
        * @return string        A public hash of the form crc32(_email)_md5(_email)
        */
        public static string email_get_public_hash(string email)
        {
            if (!string.IsNullOrEmpty(email))
            {
                email = email.ToLower().Trim();
                byte[] rawBytes = System.Text.UTF8Encoding.UTF8.GetBytes(email);

                CRC32 crc = new CRC32();
                UInt32 crcResult = crc.GetCrc32(new System.IO.MemoryStream(rawBytes));

                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] md5Result = md5.ComputeHash(rawBytes);
                string md5Data = ToHexString(md5Result).ToLower();

                return crcResult.ToString() + "_" + md5Data;
            }
            return "";
        }

        private static string ToHexString(byte[] bytes)
        {
            string hexString = "";
            for (int i = 0; i < bytes.Length; i++)
                hexString += bytes[i].ToString("X2");
            return hexString;
        }
    }
}